import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Student {
    private String name;
    private String studentId;
    private String course;
    private double score;

    public Student(String name, String studentId, String course, double score) {
        this.name = name;
        this.studentId = studentId;
        this.course = course;
        this.score = score;
    }

    // Getters
    public String getName() { return name; }
    public String getStudentId() { return studentId; }
    public String getCourse() { return course; }
    public double getScore() { return score; }
}

class GradeManager {
    private List<Student> students = new ArrayList<>();

    public boolean addStudent(String name, String studentId, String course, double score) {
        // 检查学号是否已存在
        for (Student student : students) {
            if (student.getStudentId().equals(studentId)) {
                System.out.println("错误：该学号已存在！");
                return false;
            }
        }

        // 检查成绩范围
        if (score < 0 || score > 100) {
            System.out.println("错误：成绩必须在0-100之间！");
            return false;
        }

        students.add(new Student(name, studentId, course, score));
        System.out.println("成绩已成功记录！");
        return true;
    }

    public List<Student> queryByName(String name) {
        List<Student> result = new ArrayList<>();
        for (Student student : students) {
            if (student.getName().equals(name)) {
                result.add(student);
            }
        }
        return result;
    }

    public List<Student> queryById(String studentId) {
        List<Student> result = new ArrayList<>();
        for (Student student : students) {
            if (student.getStudentId().equals(studentId)) {
                result.add(student);
            }
        }
        return result;
    }

    public List<Student> queryByCourse(String course) {
        List<Student> result = new ArrayList<>();
        for (Student student : students) {
            if (student.getCourse().equals(course)) {
                result.add(student);
            }
        }
        return result;
    }

    public void statisticsByCourse(String course) {
        List<Student> courseStudents = queryByCourse(course);

        if (courseStudents.isEmpty()) {
            System.out.println("错误：未找到该课程的成绩记录！");
            return;
        }

        double sum = 0;
        double highest = Double.MIN_VALUE;
        double lowest = Double.MAX_VALUE;

        for (Student student : courseStudents) {
            double score = student.getScore();
            sum += score;
            if (score > highest) highest = score;
            if (score < lowest) lowest = score;
        }

        double average = sum / courseStudents.size();

        System.out.printf("课程：%s%n", course);
        System.out.printf("平均分：%.2f%n", average);
        System.out.printf("最高分：%.1f%n", highest);
        System.out.printf("最低分：%.1f%n", lowest);
    }
}

